package com.health.modules.user.action;

import java.util.Collection;
import java.util.Map;

import org.ralasafe.Factory;
import org.ralasafe.WebRalasafe;
import org.ralasafe.servlet.WebUtil;
import org.ralasafe.user.UserManager;
import org.springframework.beans.factory.annotation.Autowired;

import com.health.common.Constants;
import com.health.domain.model.User;
import com.health.framework.action.BaseAction;
import com.health.modules.user.action.vo.UserVo;
import com.health.modules.user.service.UserService;
import com.opensymphony.xwork2.ActionContext;

@SuppressWarnings({"serial"})
public class UserAction extends BaseAction {
    
    @Autowired
    private UserService userService;
    
    private UserVo userVo;
    private Integer id;
    
    /**
     * 跳转到登录页
     * @return 登录页
     */
    public String toLogin() {
        return SUCCESS;
    }

    /**
     * 登录
     * @return 登录结果
     */
    public String login() {
        org.ralasafe.user.User safeUser = getSafeUser(userVo);
        
        if (null == safeUser) {
            super.request.setAttribute(Constants.Common.ERROR_MESSAGE, "用户不存在");
            return ERROR;
        } else if (!userVo.getPassword().equals(safeUser.get("pass"))) {
            super.request.setAttribute(Constants.Common.ERROR_MESSAGE, "用户账户密码错误");
            return ERROR;
        } else if (Boolean.valueOf(String.valueOf(safeUser.get(Constants.Common.IS_DELETED)))) {
            super.request.setAttribute(Constants.Common.ERROR_MESSAGE, "您的账号被禁用, 无法登录");
            return ERROR;
        } else {
            super.session.put(Constants.Session.USER, getLocalUser(safeUser));
            super.session.put(Constants.Session.SAFE_USER, safeUser);
            return SUCCESS;
        }
    }
    
    private org.ralasafe.user.User getSafeUser(UserVo userVo) {
        org.ralasafe.user.User safeUser = new org.ralasafe.user.User();
        //此处是user元数据中的字段,name, pass不能修改
        safeUser.set("name", userVo.getUsername());
        safeUser.set("pass", userVo.getPassword());
        
        UserManager manager = Factory.getUserManager(WebUtil.getUserType(super.request).getName());
        return manager.selectByUniqueFields(safeUser);
    }
    
    private User getLocalUser(org.ralasafe.user.User safeUser) {
        User user = new User();
        user.setUsername(safeUser.get("name").toString());
        user.setUserType(safeUser.get("userType").toString());
        
        return user;
    }
    
    /**
     * 跳转到用户主页面
     * @return 用户主页面
     */
    public String main() {
        return SUCCESS;
    }
    
    /**
     * 跳转到新增页面
     * @return 新增页面
     */
    public String toAdd() {
        return SUCCESS;
    }
    
    public String add() {
        ActionContext act = ActionContext.getContext();
        if (null == userVo) {
            act.put(Constants.Common.ERROR_MESSAGE, "增加用户失败");
            return ERROR;
        } else {
            User user = new User();
            user.setUsername(userVo.getUsername());
            user.setPassword(userVo.getPassword());
            user.setEmail(userVo.getEmail());
            userService.save(user);
            act.put(Constants.Common.ERROR_MESSAGE, "增加用户成功");
            return SUCCESS;
        }
    }
    
    /**
     * 跳转到编辑页面
     * @return 编辑页面
     */
    public String toEdit() {
        ActionContext act = ActionContext.getContext();
        User user = userService.findById(id);
        if (null == user) {
            act.put(Constants.Common.ERROR_MESSAGE, "用户不存在");
            return ERROR;
        } else {
            act.put("single_user", user);
            return SUCCESS;
        }
    }
    
    /**
     * 编辑用户
     * @return 编辑结果
     */
    public String edit() {
        ActionContext act = ActionContext.getContext();
        User user = userService.findById(id);
        if (null == user) {
            act.put(Constants.Common.ERROR_MESSAGE, "用户不存在");
            return ERROR;
        } else {
            user.setUsername(userVo.getUsername());
            user.setPassword(userVo.getPassword());
            user.setEmail(userVo.getEmail());
            user.setIsDelete(userVo.getIsDelete());
            
            userService.update(user);
            
            return SUCCESS;
        }
    }
    
    /**
     * 删除用户
     * @return 用户列表页
     */
    public String delete() {
        userService.removeById(id);
        
        return SUCCESS;
    }
    
    @SuppressWarnings("unchecked")
    public String list() {
        Collection<User> users = WebRalasafe.query(super.request, Constants.Privilege.USER_LIST);
        
        request.setAttribute("users", users);
        
        return SUCCESS;
    }
    
    /**
     * 退出系统
     * @return 登录页
     */
    public String logout() {
        Map<String, Object> session = ActionContext.getContext().getSession();
        session.remove(Constants.Session.USER);
        session.remove(Constants.Session.SAFE_USER);
        
        return SUCCESS;
    }

    public void setUserVo(UserVo userVo) {
        this.userVo = userVo;
    }

    public UserVo getUserVo() {
        return userVo;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

}
